package mes.generator;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.junit.Test;

/**
 * <p>
 * 测试生成代码
 * </p>
 *
 * @author 华云龙
 * @date 2018-6-14
 */
public class GeneratorServiceEntity {

    @Test
    public void generateCode() {
        /// 包名
        String packageName = "com.zx.mes.poi.hyl";
        /* user -> UserService, 设置成true: user -> IUserService */
        boolean serviceNameStartWithI = false;
        generateByTables(serviceNameStartWithI, packageName, "PCC1W7");
    }

    private void generateByTables(boolean serviceNameStartWithI, String packageName, String... tableNames) {
        GlobalConfig config = new GlobalConfig();
        String dbUrl = "jdbc:sqlserver://10.30.1.81:1433;DatabaseName=CSTCPCJC";
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.SQL_SERVER)
                .setUrl(dbUrl)
                .setUsername("sa")
                .setPassword("CSTCmaxxis")
                .setDriverName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig
                .setCapitalMode(true)
                .setEntityLombokModel(true)
//                .setDbColumnUnderline(true)
                .setEntityBuilderModel(true)
                .setNaming(NamingStrategy.underline_to_camel)
                .setSuperControllerClass("com.zx.mes.controller.BaseController")
                .setRestControllerStyle(true) // controller设置为restController
                .setInclude(tableNames);//修改替换成你需要的表名，多个表名传数组
        config.setActiveRecord(true) // 不需要ActiveRecord特性的请改为false
                .setBaseResultMap(true)
                .setBaseColumnList(true)
                .setAuthor("华云龙")
///             存放目录
                .setOutputDir("d:\\codeGen")
                .setFileOverride(true);
        if (!serviceNameStartWithI) {
            config.setServiceName("%sService");
        }
        AutoGenerator gen = new AutoGenerator();


        gen.setGlobalConfig(config)
                .setDataSource(dataSourceConfig)
                .setStrategy(strategyConfig)
                .setPackageInfo(
                        new PackageConfig()
                                .setParent(packageName)
                                .setController("controller")
                                .setEntity("entity")
                ).execute();
    }

}
